import {
  ModelOptions,
  Prop,
  getModelForClass,
  ReturnModelType,
  modelOptions
} from '@typegoose/typegoose';
import { ApiProperty, PartialType, OmitType } from '@nestjs/swagger';
import { BaseSchemas } from './base.schemas';
import { Identity } from 'src/common/utils/identity';

@modelOptions({
  schemaOptions: {
    _id: false,
    toJSON: { virtuals: true },
    toObject: { virtuals: true }
  }
})
class Other {
  @Prop()
  @ApiProperty({
    description: '',
    required: false,
    example: '****'
  })
  title?: string;

  @Prop()
  @ApiProperty({
    description: '部门',
    required: false,
    example: '销售部'
  })
  department?: string;

  @Prop()
  @ApiProperty({
    description: '职务',
    required: false,
    example: '经理'
  })
  position?: string;

  @Prop()
  @ApiProperty({
    description: '家庭地址',
    required: false,
    example: '河北省石家庄市'
  })
  homeAddress?: string;

  @Prop()
  @ApiProperty({
    description: '人物简短描述',
    required: false,
    example: '****'
  })
  description?: string;

  @Prop()
  @ApiProperty({
    description: '身高',
    example: 170,
    required: false
  })
  height?: number;

  @Prop()
  @ApiProperty({
    description: '体重',
    example: 180,
    required: false
  })
  weight?: number;

  @Prop()
  @ApiProperty({
    description: '爱好',
    example: '***',
    required: false
  })
  love?: string;

  @Prop()
  @ApiProperty({
    description: '身份证号',
    required: false,
    example: '130****'
  })
  identity?: string;

  private get idCard() {
    return new Identity(this.identity);
  }

  get age() {
    return this.idCard.age;
  }

  get gender() {
    return this.idCard.gender;
  }

  get birthday() {
    return this.idCard.birthday;
  }

  get nativePlace() {
    return this.idCard.nativePlace;
  }
}

@ModelOptions({
  schemaOptions: {
    collection: 'employee',
    timestamps: true
  }
})
export class Employee extends BaseSchemas {
  @Prop()
  @ApiProperty({
    description: '邮箱地址',
    example: 'hzq***@163.com',
    required: false
  })
  email?: string;

  @Prop()
  @ApiProperty({
    description: '手机号码',
    example: '135**',
    required: false
  })
  mobile: string;

  @Prop()
  @ApiProperty({
    description: '家庭地址',
    example: '河北省石家庄市',
    required: false
  })
  homeAddress?: string;

  @Prop()
  @ApiProperty({
    description: '单位id',
    example: '***',
    required: false
  })
  companyId?: string;

  @Prop()
  @ApiProperty({
    description: '入职日期',
    example: '2023-01-01',
    required: false,
    type: Date
  })
  employmentDate?: Date;

  @Prop({ type: Other })
  @ApiProperty({ type: Other })
  other?: Other;
}

export const EmployeeModel = getModelForClass(Employee);

export type EmployeeModelReturnType = ReturnModelType<typeof Employee>;

export class CreateEmployeeDto extends OmitType(Employee, ['nid'] as const) {}
export class UpdateEmployeeDto extends PartialType(CreateEmployeeDto) {}
